﻿using System;

namespace PengSW.TimeHelper
{
    public static class DateHelper
    {
        public static DateTime FirstDayOfThisMonth(this DateTime aDateTime) => new DateTime(aDateTime.Year, aDateTime.Month, 1);
        public static DateTime LastDayOfThisMonth(this DateTime aDateTime) => new DateTime(aDateTime.Year, aDateTime.Month, 1).AddMonths(1).AddDays(-1);
        public static DateTime FirstDayOfLastMonth(this DateTime aDateTime) => new DateTime(aDateTime.Year, aDateTime.Month, 1).AddMonths(-1);
        public static DateTime LastDayOfLastMonth(this DateTime aDateTime) => new DateTime(aDateTime.Year, aDateTime.Month, 1).AddDays(-1);

        // 约定Monday为一周的第一天，Sunday为一周的最后一天。DateTime中，SunDay的DayOfWeek为0，Monday的DayOfWeek为1。
        public static DateTime MondayOfLastWeek(this DateTime aDateTime) => aDateTime.Date.AddDays(-(((int)aDateTime.DayOfWeek + 6) % 7) - 7);
        public static DateTime SundayOfLastWeek(this DateTime aDateTime) => aDateTime.Date.AddDays(-(((int)aDateTime.DayOfWeek + 6) % 7) - 1);
        public static DateTime MondayOfThisWeek(this DateTime aDateTime) => aDateTime.Date.AddDays(-(((int)aDateTime.DayOfWeek + 6) % 7));
        public static DateTime SundayOfThisWeek(this DateTime aDateTime) => aDateTime.Date.AddDays(-(((int)aDateTime.DayOfWeek + 6) % 7) + 6);
    }
}
